<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  * {
    padding: 0;
    margin: 0;
  }

  div {
    width: 255px;
    height: 20px;
    background-color: aqua;
    margin: 0 auto;
    margin-top: 15px;
    box-sizing: border-box;
  }

  h5 {
    width: 50px;
    height: 20px;
    background-color: red;
    /* margin: 0 auto; */
    /* margin-top: 15px; */
    box-sizing: border-box;
  }

  h1 {
    border: 1px solid black;
    width: 200px;
    height: 200px;
    /* background-color: seagreen; */
    margin: 0 auto;
    position: absolute;
    top: 200px;
    left: 50%;
    transform: translateX(-50%);
  }

  .h1 {
    background-color: red;
  }

  .h2 {
    background-color: green;
  }

  .h3 {
    background-color: blue;
  }

  .h4 {
    background-color: black;
    /* background-color: rgba(red, green, blue, alpha); */
  }
</style>

<body>
  <div class="d1"> <h5 class="h1"></h5></div>
  <div class="d2"><h5 class="h2"></h5></div>
  <div class="d3"><h5 class="h3"></h5></div>
  <div class="d4"><h5 class="h4"></h5></div>
  <h1></h1>
</body>
<script>
  var box = document.querySelectorAll("div");
  var h1 = document.querySelector("h1");
  for (var i = 0; i < box.length; i++) {
    console.log(box[i]);
    box[i].addEventListener("mousemove", function (e) {
      var x = e.offsetX;
      // var y = e.offsetY;
      //  h1.innerHTML = "X轴坐标为"+x;
      this.children[0].style.width = x + "px";
      var h01 = document.querySelector(".h1").offsetWidth;
      var h02 = document.querySelector(".h2").offsetWidth;
      var h03 = document.querySelector(".h3").offsetWidth;
      var h04 = document.querySelector(".h4").offsetWidth;
      h04 = h04 / 255;
      console.log(h04);
      h1.style.backgroundColor = "rgba(" + h01 + ", " + h02 + ", " + h03 + ", " + h04 + ")";
    })
  }
</script>

</html>